package com.lzw.bluetoothpractice.Activitys;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.lzw.bluetoothpractice.Adapters.RecommendDoctorListAdapter;
import com.lzw.bluetoothpractice.Adapters.RecommendHosptialListAdapter;
import com.lzw.bluetoothpractice.Adapters.UserReadMessageListAdapter;
import com.lzw.bluetoothpractice.AppConfig;
import com.lzw.bluetoothpractice.Models.UserDoctor;
import com.lzw.bluetoothpractice.R;
import com.lzw.bluetoothpractice.Utils.AppUtil;
import com.lzw.bluetoothpractice.Utils.SharePreferenceUtils;
import com.lzw.bluetoothpractice.bean.RecommendDoctorBean;
import com.lzw.bluetoothpractice.bean.RecommendHosptialBean;
import com.lzw.bluetoothpractice.bean.SystemUserListBean;
import com.lzw.bluetoothpractice.bean.UserBean;
import com.lzw.bluetoothpractice.network.HeadAssmblyUtils;
import com.lzw.bluetoothpractice.network.RetrofitManager;
import com.lzw.bluetoothpractice.network.api.RequestApi;

import java.util.Map;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.lzw.bluetoothpractice.MyApplication.getContext;

public class DoctorsAddActivity extends BaseActivity implements View.OnClickListener {

    private Map<String, String> keyMap;
    private RecommendDoctorListAdapter recommendDoctorListAdapter;
    private RecyclerView rv_recommendDoctor;

     RecommendHosptialListAdapter recommendHosptialListAdapter;
     RecyclerView rv_hosptialList;
     EditText doctor_name;
     Button bt_Serach;
     ImageView detail_doctor_head;
     TextView detail_doctor_name;
     TextView detail_doctor_introduce;
     TextView detail_doctor_phone;
     TextView detail_doctor_id;
     Button doctor_add;
     LinearLayout doctor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctors_add);
        InitNavBar2(true,"查找医生",false);
        rv_recommendDoctor=this.findViewById(R.id.rv_doctorList);
        rv_hosptialList=this.findViewById(R.id.rv_hosptialList);
        doctor=this.findViewById(R.id.doctor);
        InitView();


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_Serach:
                initSerach();
                break;
            case R.id.doctor_add:   //进行注册
                doctorAdd();
                break;

        }
    }
    private void InitView(){
        doctor_name=findViewById(R.id.doctor_name);
        bt_Serach=this.findViewById(R.id.bt_Serach);
        detail_doctor_head=this.findViewById(R.id.detail_doctor_head);
        detail_doctor_name=findViewById(R.id.detail_doctor_name);
        detail_doctor_introduce=findViewById(R.id.detail_doctor_introduce);
        detail_doctor_phone=findViewById(R.id.detail_doctor_phone);
        detail_doctor_id=findViewById(R.id.detail_doctor_id);
        doctor_add=this.findViewById(R.id.doctor_add);
        doctor_add.setOnClickListener(this);
        bt_Serach.setOnClickListener(this);


    }
    private void initSerach(){
        try {
            keyMap = HeadAssmblyUtils.HeadAssembly("/dev-api/android/user/recommendDoctor");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String accessToken = SharePreferenceUtils.getString(getContext(),"access_token",null);;

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("user_name",doctor_name.getText().toString().trim());
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).getDoctorByName(jsonObject,"1", AppUtil.getVersionCode() + "", accessToken ,keyMap.get("gmtStr"), keyMap.get("contentMD5"), keyMap.get("hmacCode")).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e("查找医生",e.getMessage()+"");

            }

            @Override
            public void onNext(JSONObject json) {

                UserBean userBean = JSON.parseObject(json.toString(), UserBean.class);
                if(userBean.getCode()==200){
                    if(userBean.getData()==null){
                        doctor.setVisibility(View.GONE);
                    }else {
                        doctor.setVisibility(View.VISIBLE);
                        Glide.with(DoctorsAddActivity.this).load(AppConfig.BASE_URL+userBean.getData().getAvatar()).into(detail_doctor_head);
                        detail_doctor_name.setText(userBean.getData().getNickName());
                        detail_doctor_introduce.setText(userBean.getData().getDescribe());
                        detail_doctor_phone.setText(userBean.getData().getPhonenumber());
                        detail_doctor_id.setText(userBean.getData().getUserId().toString());
                    }







                }else {
                    Toast toast=Toast.makeText(DoctorsAddActivity.this,"查询医生失败",Toast.LENGTH_LONG);
                    toast.show();
                }


            }
        });

    }
    private void doctorAdd(){
        try {
            keyMap = HeadAssmblyUtils.HeadAssembly("/dev-api/android/user/recommendDoctor");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String accessToken = SharePreferenceUtils.getString(getContext(),"access_token",null);;
        String userid = SharePreferenceUtils.getString(getContext(),"userId",null);
        UserDoctor userDoctor=new UserDoctor();
        userDoctor.setUsdUserId(Long.parseLong(userid));
        userDoctor.setUsdDoctorId(Long.parseLong(detail_doctor_id.getText().toString().trim()));
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).addUserDoctor(userDoctor,"1", AppUtil.getVersionCode() + "", accessToken ,keyMap.get("gmtStr"), keyMap.get("contentMD5"), keyMap.get("hmacCode")).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e("查找医生",e.getMessage()+"");

            }

            @Override
            public void onNext(JSONObject json) {

                UserBean userBean = JSON.parseObject(json.toString(), UserBean.class);
                if(userBean.getCode()==200){


                    Toast toast=Toast.makeText(DoctorsAddActivity.this,"绑定医生成功",Toast.LENGTH_LONG);
                    toast.show();




                }else {
                    Toast toast=Toast.makeText(DoctorsAddActivity.this,"查询医生失败",Toast.LENGTH_LONG);
                    toast.show();
                }


            }
        });

    }



}